<?=view('admin.common.header')?>
<?=view('admin.common.nav')?>
<div class="container">
    <div class="row">
        <div class="panel panel-default">
            <div class="panel-heading">
                发布新任务
            </div>
            <div class="panel-body">
                <?php if ($request->has('oid') && $order): ?>
                <div class="col-xs-12 col-sm-12">
                    <form id="jobadd" class="form-horizontal mtop15" method="post">
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">用户订单号</label>
                            <div class="col-sm-10">
                                <input type="text" name="ordernum" class="form-control" readonly="readonly" value="<?=$order->order?>"/>
                            </div>
                          </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">装机小区</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" disabled="disabled" value="<?=$order->strPlot()?>"/>
                                <input type="hidden" value="<?=$order->plotid?>" name="plotid"/>
                            </div>
                          </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">装维师傅</label>
                            <div class="col-sm-10">
                                <select class="form-control" name="iperid">
                                    <option value="">--请选择装维师傅--</option>
                                    <?=$order->strPlotIper()?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">客户名称</label>
                            <div class="col-sm-10">
                                <input type="text" value="<?=$order->name?>" readonly="readonly"  name="cname" class="form-control" placeholder="--请填写故障客户姓名--" required="required">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">联系电话</label>
                            <div class="col-sm-10">
                                <input type="text" value="<?=$order->phone?>" readonly="readonly"  name="cphone" class="form-control" placeholder="--请填写故障客户联系电话--" required="required">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">业务类型</label>
                            <div class="col-sm-10">
                                <input type="text" value="<?=$order->strProductType()?>" name="btype" class="form-control" placeholder="--任务类型 如：新装--" required="required">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">装机地址</label>
                            <div class="col-sm-10">
                                <textarea name="address" class="form-control" readonly="readonly"  style="max-width: 100%; min-height: 130px;" placeholder="--用户详细装机地址--" required="required"><?=$order->address?></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button class="btn btn-success eveAdd" onclick="return false;">派发任务</button>
                            </div>
                        </div>
                    </form>
                </div>
                <?php else: ?>
                <div class="col-xs-12 col-sm-12">
                    <div class="kdsl">选择地区：</div>
                    <div class="col-xs-12 col-md-12 area">
                        <?php if ($area && count($area)): ?>
                        <?php foreach ($area as $val): ?>
                        <a href="<?=url("admin/jobs/add?area={$val->id}")?>" <?php if ($val->id == $request->input('area', 1)): ?>class="active"<?php endif; ?>><?=$val->name?></a>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="col-xs-12 col-sm-12">
                    <form id="jobadd" class="form-horizontal mtop15" method="post">
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">装机小区</label>
                            <div class="col-sm-10">
                                <select class="form-control" name="plotid">
                                    <option value="">--请选择小区--</option>
                                    <?php if ($plots && count($plots)): ?>
                                    <?php foreach ($plots as $val): ?>
                                    <option value="<?=$val->id?>"><?=$val->name?></option>
                                    <?php endforeach; ?>
                                    <?php endif; ?>
                                </select>
                            </div>
                          </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">装维师傅</label>
                            <div class="col-sm-10">
                                <select class="form-control" name="iperid">
                                    <option value="">--请选择装维师傅--</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">客户名称</label>
                            <div class="col-sm-10">
                                <input type="text" value="" name="cname" class="form-control" placeholder="--请填写故障客户姓名--" required="required">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">联系电话</label>
                            <div class="col-sm-10">
                                <input type="text" value="" name="cphone" class="form-control" placeholder="--请填写故障客户联系电话--" required="required">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">业务类型</label>
                            <div class="col-sm-10">
                                <input type="text" value="" name="btype" class="form-control" placeholder="--任务类型 如：新装--" required="required">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">装机地址</label>
                            <div class="col-sm-10">
                                <textarea name="address" class="form-control" style="max-width: 100%; min-height: 130px;" placeholder="--用户详细装机地址--" required="required"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button class="btn btn-success eveAdd" onclick="return false;">派发任务</button>
                            </div>
                        </div>
                    </form>
                </div>
                <?php endif; ?>
            </div>
        </div> 
    </div>
</div>
<script>
    $('select[name=plotid]').change(function(){
        var plotid = $(this).val();
        if (plotid == '') {
            layer.msg("请选择一个小区");
            $('select[name=iperid]').html("<option value=''>--请选择装维师傅--</option>");
            return false;
        }
        $.get('<?= url("admin/jobs/plotiper") ?>', {'plotid': plotid}, function(data){
            if (data.status == 0) {
                layer.msg(data.info);
                $('select[name=iperid]').html("<option value=''>--请选择装维师傅--</option>");
            } else if (data.status == 1) {
                var i = 0; options="<option value=''>--请选择装维师傅--</option>";
                for (; i < data.info.length; i++) {
                    options += "<option value='"+data.info[i].id+"'>"+data.info[i].name+"</option>";
                }
                $('select[name=iperid]').html('');
                $('select[name=iperid]').html(options);
            }
        });
    });
    $('.eveAdd').click(function(){
        if ($(this).attr('disabled') ==  'disabled')
            return false;
        if (!(checkNull($("input[name=plotid]"), "请选择一个小区") && checkNull($("select[name=iperid]"), "请选择一个装维师傅") && 
              checkNull($("input[name=cname]"), "请填写客户姓名") && checkNull($("input[name=cphone]"), "请填写客户联系电话") && 
              checkNull($("input[name=btype]"), "请填写任务类型") && checkNull($('textarea[name=address]'), "请填写维修地址"))) {
            return false;
        }
        $(this).attr('disabled', 'disabled');
        $('#jobadd').ajaxSubmit(function(data){
            layer.msg(data.info);
            if (data.status == 1) {
                window.location.href='<?=url("admin/jobs/index")?>';
            } else if (data.status == 1) {
                $('.eveAdd').removeAttr('disabled');
            }
        });
    });
    
    function checkNull(elem, msg) {
        if (elem.val() == '') {
            layer.msg(msg);
            elem.focus();
            return false;
        }
        return true;
    }
</script>
<?=view('admin.common.footer')?>